package com.hitqz.robot.dispatch.netty.strategy.zhiqingDispather;

import com.hitqz.robot.api.dispatch.protocol.dto.RobotDto;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.*;

@Slf4j
@Data
public class BlockedContext {
    public BlockedContext(Double blockRadio, Geometry rectPolygon) {
        GeometryFactory geometryFactory = new GeometryFactory();

        // 定义原始四边形坐标点
        Coordinate[] coordinates = new Coordinate[]{
                new Coordinate(5.524, 5.655),  // 点1
                new Coordinate(5.615, 0.850),  // 点2
                new Coordinate(10.587, 0.85),  // 点3
                new Coordinate(10.587, 5.655), // 点4
                new Coordinate(5.524, 5.655)   // 闭合回点1
        };

        // 创建原始多边形
        LinearRing shell = geometryFactory.createLinearRing(coordinates);
        Polygon originalPolygon = geometryFactory.createPolygon(shell);

        this.bufferedPolygon = originalPolygon.buffer(blockRadio);
        if (rectPolygon != null) {
            this.bufferedPolygon = this.bufferedPolygon.difference(rectPolygon);
        }
    }

     Geometry bufferedPolygon;


    public static volatile Boolean isBlocked = false;

    public static RobotDto blockRobot;

    public static final Object lock = new Object();

    public static synchronized Boolean apply(RobotDto dto) {

        //已经在阻塞中的直接返回
        if (isBlocked && blockRobot == dto) {
            return true;
        }
        log.info("{}申请阻塞区域开始", dto);
        if (!isBlocked) {
            isBlocked = true;
            blockRobot = dto;
            log.info("{}申请阻塞区域成功", dto);
            return true;
        }


        log.info("{}申请阻塞区域失败", dto);
        return false;

    }

    /**
     * 释放阻塞区域
     *
     * @param dto
     */
    public static void release(RobotDto dto) {
        synchronized (lock) {
            log.info("{}:{}走出阻塞区域，释放锁,", dto.getRobotName(), dto.getRobotSn());
            isBlocked = false;
            blockRobot = null;
            lock.notify();

        }
    }

    public static void main(String[] args) {
        GeometryFactory geometryFactory = new GeometryFactory();

        // 定义原始四边形坐标点
        Coordinate[] coordinates = new Coordinate[]{
                new Coordinate(5.524, 5.655),  // 点1
                new Coordinate(5.615, 0.850),  // 点2
                new Coordinate(10.587, 0.85),  // 点3
                new Coordinate(10.587, 5.655), // 点4
                new Coordinate(5.524, 5.655)   // 闭合回点1
        };
        Coordinate[] rectCoords = new Coordinate[]{
                new Coordinate(4.0, 8.0),
                new Coordinate(4.0, 9.0),
                new Coordinate(10.0, 9.0),
                new Coordinate(10.0, 8.0),
                new Coordinate(4.0, 8.0)  // 闭合
        };
        LinearRing rectRing = geometryFactory.createLinearRing(rectCoords);
        Polygon rectPolygon = geometryFactory.createPolygon(rectRing);
        // 创建原始多边形
        LinearRing shell = geometryFactory.createLinearRing(coordinates);
        Polygon originalPolygon = geometryFactory.createPolygon(shell);
        Geometry buffer = originalPolygon.buffer(2.5);

        Geometry difference = buffer.difference(rectPolygon);
        System.out.println(difference);

    }





    /*
     *//**
     * 进入C点释放锁
     *//*

    public static  Path pathCXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathCYLimit = new Path(0, -10, 0, 10);

    *//**
     * 进入H点释放锁
     *//*
    public static  Path pathHXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathHYLimit = new Path(0, -10, 0, 10);


    *//**
     * 进入H点释放锁
     *//*
    public static  Path pathIXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathIYLimit = new Path(0, -10, 0, 10);*/

    /*
     *//**
     * 进入H点释放锁
     *//*
    public static  Path pathDXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathDYLimit = new Path(0, -10, 0, 10);


    *//**
     * 进入H点释放锁
     *//*
    public static  Path pathEXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathEYLimit = new Path(0, -10, 0, 10);


    public static  Path pathGXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathGYLimit = new Path(0, -10, 0, 10);

    public static  Path pathFXLimit = new  Path(-10, 0, 10, 0);
    public static  Path pathFYLimit = new Path(0, -10, 0, 10);*/
}

